package com.cipe.cmrs.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;

import com.cipe.cmrs.bean.CipeprojectBean;
import com.cipe.cmrs.model.Cipeproject;
import com.cipe.cmrs.model.Donoraward;
import com.cipe.cmrs.util.Constants;
import com.cipe.cmrs.util.DateUtil2;

/**
 * @author Incepio
 * 
 */
public class CipeProjectToCipeProjectBean implements Converter<Cipeproject, CipeprojectBean> {

    private ConverterRegistry converterRegistry;

    /**
     * @param converterRegistry
     *            the converterRegistry to set
     */
    public void setConverterRegistry(ConverterRegistry converterRegistry) {
        this.converterRegistry = converterRegistry;
    }

    public void init() {
        converterRegistry.addConverter(this);
    }

    public CipeprojectBean convert(Cipeproject source) {
        CipeprojectBean bean = null;
        if (source != null) {
            bean = new CipeprojectBean();
            bean.setNedIdentifier(source.getNedIdentifier());
            Donoraward donorAward = source.getDonoraward();
            if (donorAward != null) {
                bean.setDonorName(donorAward.getDonorAwardDesc());
                bean.setDonorAwardNumber(donorAward.getDonorAwardNumber());
                bean.setDonorAwardStartDate(donorAward.getStartDate());
                bean.setDonorAwardEndDate(donorAward.getEndDate());
            }
            bean.setTitle(source.getProjectTitle());
            bean.setCostCenterNumber(source.getCostCenterNumber());
            bean.setId(source.getId());
            bean.setProjectId(source.getProjectID());
            if (source.getCipeprojecttype() != null) {
                bean.setProjectType(source.getCipeprojecttype().getDisplayName());
                if (Constants.PartnerGrant.equalsIgnoreCase(source.getCipeprojecttype().getDisplayName())) {
                    bean.setPartnerGrant(true);
                }
            }
            if (source.getCipeprojectstatus() != null) {
                bean.setStatus(source.getCipeprojectstatus().getDisplayName());
            }
            if (source.getRegion() != null) {
                bean.setRegion(source.getRegion().getDisplayName());
            }
            if (source.getCountry() != null) {
                bean.setCountry(source.getCountry().getDisplayName());
            }
            bean.setAcronym(source.getAcronym());
            if (source.getProjectStartDate() != null) {
                bean.setStartDate(source.getProjectStartDate());
                bean.setFormattedStartDate(DateUtil2.INSTANCE.formatDate(DateUtil2.DEFAULT_PATTERN, source.getProjectStartDate()));
            }
            if (source.getProjectEndDate() != null) {
                bean.setEndDate(source.getProjectEndDate());
                bean.setFormattedEndDate(DateUtil2.INSTANCE.formatDate(DateUtil2.DEFAULT_PATTERN, source.getProjectEndDate()));
            }
            if (source.getPartner() != null) {
                bean.setPartnerName(source.getPartner().getPartnerName());
            }
            bean.setOverride(source.getOverride() != null && source.getOverride());
            if (source.getManagementFunds() != null) {
                bean.setManagementFunds(source.getManagementFunds());
            }
            if (source.getPartnerGrantFunds() != null) {
                bean.setPartnerGrantFunds(source.getPartnerGrantFunds());
            }
        }
        return bean;
    }
}
